package com.mdx.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static java.util.stream.Collectors.toList;

import org.junit.jupiter.api.Test;

public class StreamPractice {
			
	static class Person {
		String name;
		String email;
		
		public Person(String name, String email) {
			this.name = name;
			this.email = email;
		}
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getEmail() {
			return email;
		}
		public void setEmail(String email) {
			this.email = email;
		}
	}
	
	@Test
	public void testMap() {
		List<Map<Integer, Integer>> list = new ArrayList<>();
		Map<Integer, Integer> map = new HashMap<>();
		map.put(10,10);
		list.add(map);
		list.stream().map(streamData -> {
			streamData.put(20,20);
			return streamData;
		}).collect(toList());
		//System.out.println(list);
		
		List<Person> plList = new ArrayList<>();
		plList.add(new Person("lilei", "lilei@qq.com"));
		System.out.println(plList.get(0).getEmail());
		plList.stream().map(mapper -> {
			if (mapper.getName().equals("lilei"))
				mapper.setEmail("hanmeimei@qq.com");
			return mapper;
		}).collect(toList());
		System.out.println(plList.get(0).getEmail());
	}
	
	@Test
	public void testFilter() {
		Integer[] arr = {1,2,3,4,5,6,7,8,9};
		List<Integer> list = new ArrayList<>(Arrays.asList(arr));
		List<Integer> result = list.stream().map(mapper -> {
			return mapper * 2;
		}).collect(toList());
		System.out.println(list);
		System.out.println(result);
		
		//----------------------------
		
		String[] sarr = {"a","b","c","d","e"};
		List<String> sList = new ArrayList<>(Arrays.asList(sarr));
		sList.stream().map(s -> s.toUpperCase()).forEach(System.out::print);
		System.out.println(sList);
	}

}
